package com.xt.shop.Controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xt.shop.Model.AdminInfo;
import com.xt.shop.Model.LoginRequest;
import com.xt.shop.Model.RestBean;
import com.xt.shop.service.impl.AdminInfoServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/admin")
public class AdminController {

    @Resource
    AdminInfoServiceImpl adminInfoService;

    @RequestMapping("/login")
    public RestBean Login(@RequestBody LoginRequest request){
        if (adminInfoService.Login(request)){
            return RestBean.success(adminInfoService.getOne(new QueryWrapper<AdminInfo>()
                    .eq("username",request.getUsername())));
        }
        return RestBean.failure(400,"登录失败");
    }

    @RequestMapping("/register")
    public RestBean register(@RequestBody LoginRequest request){
        if (adminInfoService.register(request)>0){
            return RestBean.success(adminInfoService.getOne(new QueryWrapper<AdminInfo>()
                    .eq("username",request.getUsername())));
        }
        return RestBean.failure(400,"注册失败");
    }

    @RequestMapping("/get")
    public RestBean userinfo(String uid){
        AdminInfo byId = adminInfoService.getById(uid);
        if (byId != null){
            return RestBean.success(byId);
        }
        return RestBean.failure(400,"不存在该用户");
    }




}
